str = "Hello world! This is standard lib of string."

-- !!!
-- !!!
-- the operation for a string will not modify the original string
-- execept that you reassign it

-- get the length of a string
print("length: " .. string.len(str))
print("length: " .. #str)

-- repeat the string n times
print("repeat: " .. string.rep(str, 2)) 	-- repeat 2 times of str

-- reverse the string
print("reverse: " ..string.reverse(str))

-- change the character to lower case
print("lower case: " .. string.lower(str))

-- change the character to upper case
print("upper case: " .. string.upper(str))

-- get the substring of a string
-- note that the index used is only the representative position,
-- so the position of the first index representative should be before or equal to 
-- the position of the second index representative
print("substring: " .. string.sub(str, 1, 5)) 	-- a string from the first character to the fifth character
print("substring: " .. string.sub(str, 1, -1)) 	-- minus number means indexing from the end of the string

-- number to character
print(string.char(97)) 	-- a (97)
i = 99; print(string.char(i, i + 1, i + 2)) 	-- cde

-- character to number
print(string.byte("abc")) 		-- 97 (a) default is 1
print(string.byte("abc", 1)) 	-- 97 (a)
print(string.byte("abc", 2)) 	-- 98 (b)
print(string.byte("abc", -1)) -- 99 (c)
print(string.byte("abcdefg", 1, -1))

-- formatted output string
-- %d means decimal integer
-- %x means lower hexadecimal number
-- %X means upper hexadecimal number
-- %f means a float point number
-- %s means a string
print(string.format("x = %d, y = %d", 10, 20)) 	-- x = 10, y = 20
print(string.format("x = %x", 200)) 						-- x = c8
print(string.format("x = 0x%X", 200)) 					-- x = 0xC8
print(string.format("x = %f", 200)) 						-- x = 200.000000
print(string.format("<%s>%s<%s>", "1", str, "2"))

-- find a string
print(string.find(str, "wor")) 	-- output: start   end

-- replace a string
print(string.gsub(str, "l", "."))	-- replace "l" to "." 
																	-- output the replaced string and replace times
